﻿//  --------------------------------
//  Copyright (c) PK Logix LLC. All rights reserved.
//  This source code is made available under the terms of the GNU General Public License version 2 (GPLv2)
//  http://www.codeplex.com/mvcclubsite/license
//  ---------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MVCClubSite.IDAL;
using MVCClubSite.Model;

namespace MVCClubSite.BLL
{
    /// <summary>
    /// This is the business component to manage albums
    /// </summary>
    public class AlbumsService
    {
        IAlbums _albums;
        IImages _images;

        /// <summary>
        /// Default constructor that will use the DAL set in the web config
        /// </summary>
        public AlbumsService()
        {
            _albums = MVCClubSite.DALFactory.DataAccess.CreateAlbums();
            _images = MVCClubSite.DALFactory.DataAccess.CreateImages();
        }

        /// <summary>
        /// Constructor for testing with a different DAL
        /// </summary>
        /// <param name="announcements">DAL for the announcement service to use</param>
        public AlbumsService(IAlbums albums, IImages images)
        {
            _albums = albums;
            if (_albums == null)
                throw new InvalidOperationException("Albums DAL cannot be null.");

            _images = images;
            if (_images == null)
                throw new InvalidOperationException("Images DAL cannot be null.");
        }

        /// <summary>
        /// Method to get all albums
        /// </summary>
        /// <returns>A list of albums</returns>
        public List<Album> GetAllAlbums()
        {
            List<Album> albums = _albums.GetAllAlbums().ToList();

            foreach (Album a in albums)
            {
                a.ThumbnailID = _images.GetAlbumThumbnailID(a.ID);
            }
            return  albums;
        }

        /// <summary>
        /// Method to get public albums
        /// </summary>
        /// <returns>A list of albums</returns>
        public List<Album> GetPublicAlbums()
        {
            List<Album> albums = _albums.GetPublicAlbums().ToList();

            foreach (Album a in albums)
            {
                a.ThumbnailID = _images.GetAlbumThumbnailID(a.ID);
            }
            return albums;
        }

        /// <summary>
        /// Method to get an ablum by and album ID
        /// </summary>
        /// <param name="id">ID of the album to return</param>
        /// <returns>An album business entity</returns>
        public Album GetAlbumByID(int id)
        {
            return _albums.GetAlbumByID(id);
        }

        /// <summary>
        /// Method to insert or update an album
        /// </summary>
        /// <param name="album">Album to insert or update</param>
        public void Save(Album album)
        {
            _albums.Save(album);
        }

        /// <summary>
        /// Method to delete an album
        /// </summary>
        /// <param name="id">ID of the album to delete</param>
        public void Delete(int id)
        {
            _images.DeleteByAlbumID(id);
            _albums.Delete(id);
        }

    }
}
